package tools;

import funcktion.UserManage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class UserUpdateGUI extends JFrame {
    private JTextField userIdTextField;
    private JTextField userNameTextField;
    private JTextField genderTextField;
    private JTextField borrowedBooksTextField;
    private JTextField maxBorrowedBooksTextField;
    private JTextField statusTextField;
    private JButton saveButton;

    public UserUpdateGUI(String userId) {
        setTitle("读者信息");
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2, 10, 10));

        JLabel userIdLabel = new JLabel("用户证号");
        userIdTextField = new JTextField();
        JLabel userNameLabel = new JLabel("用户名称");
        userNameTextField = new JTextField();
        JLabel genderLabel = new JLabel("性别");
        genderTextField = new JTextField();
        JLabel BorrowedBooksLabel = new JLabel("借书量");
        borrowedBooksTextField = new JTextField();
        JLabel maxBorrowedBooksLabel = new JLabel("可借最大数量");
        maxBorrowedBooksTextField = new JTextField();
        JLabel statusLabel = new JLabel("状态");
        statusTextField = new JTextField();

        saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateUserInfo(userId);
            }
        });

        add(userNameLabel);
        add(userNameTextField);
        add(genderLabel);
        add(genderTextField);
        add(BorrowedBooksLabel);
        add(borrowedBooksTextField);
        add(maxBorrowedBooksLabel);
        add(maxBorrowedBooksTextField);
        add(statusLabel);
        add(statusTextField);
        add(new JLabel());
        add(saveButton);

        pack();//根据窗口中包含的组件自动调整窗口的大小，以适应所有组件的尺寸。
        setLocationRelativeTo(null);

        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "lang520520");
             PreparedStatement stmt1 = conn.prepareStatement("SELECT * FROM User WHERE userId = ?")) {
            // 设置参数并执行操作
            stmt1.setString(1, userId);
            ResultSet rs = stmt1.executeQuery();
            System.out.println(rs);

            if (rs.next()) {
                // 处理结果集的数据
                //System.out.println(rs.getString("userName"));
                // 将查询结果放入文本框
                userNameTextField.setText(rs.getString("userName"));
                genderTextField.setText(rs.getString("gender"));
                borrowedBooksTextField.setText(String.valueOf(rs.getInt("borrowedBooks")));
                maxBorrowedBooksTextField.setText(String.valueOf(rs.getInt("maxBorrowedBooks")));
                statusTextField.setText(rs.getString("status"));

            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }





    private void updateUserInfo(String userId) {
        String userName = userNameTextField.getText();
        String gender = genderTextField.getText();
        int borrowedBooks = 0;
        int maxBorrowedBooks = 0;
        String status = statusTextField.getText();



        if (userName.isEmpty() || status.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段。");
        }else if (gender != "男" && gender != "女") {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段。");
        }else {
            try {
                System.out.println(borrowedBooksTextField.getText());
                borrowedBooks = Integer.parseInt(borrowedBooksTextField.getText());
                maxBorrowedBooks = Integer.parseInt(maxBorrowedBooksTextField.getText());

                // 保存图书信息到数据库
                // 检查必填字段是否为空

                try {
                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "lang520520");
                    PreparedStatement stmt = conn.prepareStatement("UPDATE User SET userName = ?, gender = ?, borrowedBooks = ?, maxBorrowedBooks = ?, status = ? WHERE userId = ?");
                    stmt.setString(6, userId);
                    stmt.setString(1, userName);
                    stmt.setString(2, gender);
                    stmt.setInt(3, borrowedBooks);
                    stmt.setInt(4, maxBorrowedBooks);
                    stmt.setString(5, status);

                    stmt.executeUpdate();

                    JOptionPane.showMessageDialog(this, "读者信息保存成功");

                    UserManage GUI = new UserManage();
                    GUI.setVisible(true);
                    setVisible(false);
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(this, "保存失败：" + e.getMessage());
                    e.printStackTrace();
                }

            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "请输入有效的数字");
                e.printStackTrace();
            }
        }
    }

}
